package dolf.objects.weapons.DriveByShotgun;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;

import dolf.game.Level;
import dolf.game.Player;
import dolf.objects.Bullet;
import dolf.objects.ImageStore;
import dolf.objects.Point;
import dolf.objects.Vector;
import dolf.objects.weapons.*;
import dolf.window.SystemMessagesHandler;

public class DriveByShotgun implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 90;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByShotgun.lang.lang").getString("name");;
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByShotgun.lang.lang").getString("type");;
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByShotgun.lang.lang").getString("description");;
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/DriveByShotgun/gfx/icon.png");
	
	private int bullets;

	private Player pl;
	private boolean inititalized = false;
	private boolean triggerPressed;
	private int timer;
	private final int MAX_TIMER = 20;
	private final int MAX_BULLETS = 7;
	private transient Cursor crosshair;
	public DriveByShotgun(){
		Image c = ImageStore.getInstance().getImage("dolf/gfx/environment/crosshair.png");
		crosshair = Toolkit.getDefaultToolkit().createCustomCursor(c,new java.awt.Point(10,10), "Cursor" );		
	}
	
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		
		this.inititalized = true;
		this.timer = 0;
		bullets = this.MAX_BULLETS;
		this.pl = _pl;
		this.pl.setWeaponState();
		this.triggerPressed = false;
	}
	public void deInitialize(){
		dolf.window.Canvas.getInstance().setCursor(null);
		this.inititalized = false;
		this.pl = null;
	}
	
	public void execute() {
		if (pl.getBall().isMoving() && bullets > 0) {
			if (crosshair == null) {
				Image c = ImageStore.getInstance().getImage("dolf/gfx/environment/crosshair.png");
				crosshair = Toolkit.getDefaultToolkit().createCustomCursor(c,new java.awt.Point(10,10), "Cursor" );
			}
			dolf.window.Canvas.getInstance().setCursor(crosshair);
		}
		else dolf.window.Canvas.getInstance().setCursor(null);
		
		if(this.triggerPressed && !Level.getInstance().mousePressed()){
			this.triggerPressed = false;
		}
		if(Level.getInstance().mousePressed()&&!this.triggerPressed &&pl.getBall().isMoving()&&timer<=0){
			if(this.bullets > 0){
				//Point pos = pl.getBall().getPosition().sub(pl.getBall().getPosition().sub(Level.getInstance().getMouse()).setLength(pl.getBall().getRadius()+3));
				double ang = Level.getInstance().getMouse().sub(pl.getBall().getPosition()).getAngle()-(Math.PI*4/45);
				for(int i = 0; i < 7; i++){
					ang += Math.PI/45;
					Vector vec = new Vector(Math.cos(-ang),Math.sin(-ang));
					Point pos = pl.getBall().getPosition().add(vec.setLength(pl.getBall().getRadius()+pl.getBall().getMotion().getLength()+1));
					Bullet b = new Bullet(pos, vec.setLength(7), this.pl,1); 	
					Level.getInstance().registerObjectOnTop(b);
				}
				this.bullets--;
				this.pl.setWeaponState();
				this.timer = this.MAX_TIMER;
				this.triggerPressed = true;
				return;
			} 
		} else {
			if(this.timer > 0){
				this.timer--;
			}
		}	
	}

	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/DriveByShotgun/gfx/icon.png");	
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	
	public String getType(){
		return WeaponType;
	}

	public int getPrice() {
		return Price;
	}

	public Player getPlayer(){
		return this.pl;
	}
	
	public void remove(){
		dolf.window.Canvas.getInstance().setCursor(null);
		this.pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public double getState(){
		return (double)(this.bullets)/this.MAX_BULLETS;
	}
}
